package com.fishery.order.controller;


import com.fishery.entity.Result;
import com.fishery.order.entity.Customer;
import com.fishery.order.entity.vo.CustomerVo;
import com.fishery.order.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author run 、 ghost
 * @since 2021-01-31
 */
@Api(tags = "顾客管理相关接口")
@CrossOrigin
@RestController
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;


	@PostMapping
	@ApiOperation("添加顾客信息")
	public Result createCustomer(@RequestBody @ApiParam(required = true, value = "customer") Customer customer) {
		return customerService.createCustomer(customer)
				? Result.success("添加成功") : Result.error("添加失败");
	}

	@PutMapping
	@ApiOperation("修改顾客信息")
	public Result updateCustomer(@RequestBody @ApiParam(required = true, value = "customerVo") CustomerVo customerVo) {
		if (customerVo == null || StringUtils.isEmpty(customerVo.getId())) {
			return Result.error("修改失败");
		}
		return customerService.updateCustomerInfo(customerVo)
				? Result.success("修改成功") : Result.error("修改失败");
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除顾客信息")
	public Result deleteCustomer(@PathVariable(value = "id") String customerId) {
		if (StringUtils.isEmpty(customerId)) {
			return Result.error("删除失败");
		}
		return customerService.deleteCustomer(customerId)
				? Result.success("删除成功") : Result.error("删除失败");
	}

	@GetMapping("/{id}")
	@ApiOperation("获取基地的全部顾客信息")
	public Result searchCustomerOfBase(@PathVariable(value = "id") String baseId) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		return Result.success()
				.data(customerService.searchCustomerOfBase(baseId));
	}

	@GetMapping("/{id}/{page}/{size}")
	@ApiOperation("分页获取基地的顾客信息")
	public Result searchCustomerOfBase(@PathVariable(value = "id") String baseId,
									   @PathVariable(value = "page") Integer page,
									   @PathVariable(value = "size") Integer size) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		return Result.success()
				.data(customerService.searchCustomerOfBase(baseId, page, size));
	}

	@GetMapping("/top/{baseId}/{size}")
	@ApiOperation("获取基地前N个大客户（领导仓）")
	public Result searchTopCustomerOfBase(@PathVariable(value = "baseId") String baseId,
										  @PathVariable(value = "size") Integer size) {
		if (StringUtils.isEmpty(baseId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(customerService.getTopCustomerOfBase(baseId, size));
	}


}

